import BaseEvent from '@/components/events/BaseEvent'
import { isWeibo } from '@/plugins/utils'
import { COMPONENT_TYPES, CONTROL_TYPES } from '@/common/const-wb'
import { isHasOwnProperty } from '@/plugins/utils'

export default class FollowEvent extends BaseEvent{
  constructor() {
    super();
  }
  
  excute({item, event, controlData}){
    this.item = item;
    this.event = event;

    let followUrl = "";
    let follow_uid = controlData.data.follow_uid

    //3中情况： 1 有全局关注（一个全局关注单独控制一个按钮关注） 2 有全局关注 控制所有关注事件 3 是没有全局关注的
    if ((isHasOwnProperty(this.globalIsPost.isFollowClickObj, follow_uid) && this.globalIsPost.isFollowClickObj[follow_uid]) ||
        isHasOwnProperty(this.globalIsPost, 'isFollow') && this.globalIsPost.isFollow || 
        !isHasOwnProperty(this.globalIsPost, 'isFollow') && !isHasOwnProperty(this.globalIsPost.isFollowClickObj, follow_uid)){
      if(!this.globalIsPost.isFollowedObj[follow_uid]) {
        let follow_uids = []
        if(this.item.cid == COMPONENT_TYPES.wb_common_list){
          if (this.getItemControls[CONTROL_TYPES.wb_user]) {
            follow_uid = this.getItemControls[CONTROL_TYPES.wb_user].data.elements[this.commonAttr.itemIndex].uid
          } else if (this.getItemControls[CONTROL_TYPES.wb_reposts]) {
            let repostItem = this.getItemControls[CONTROL_TYPES.wb_reposts].data.elements[this.commonAttr.itemIndex]
            follow_uid = repostItem.uid
          }
        } else {
          let elements = null;
          let controlData;
          if (this.getItemControls[CONTROL_TYPES.wb_user]) {
            controlData = this.getItemControls[CONTROL_TYPES.wb_user].data
          } else if (this.getItemControls[CONTROL_TYPES.wb_reposts]) {
            controlData = this.getItemControls[CONTROL_TYPES.wb_reposts].data
          }
          elements = controlData && controlData.elements;
          if(elements){
            if(controlData.isFollowByIndex && this.commonAttr.itemIndex >= 0){
              follow_uid = elements[this.commonAttr.itemIndex].uid
            } else {
              _.forEach(elements, (item) => {
                if(item.uid || item.id){
                  follow_uids.push(item.uid || item.id)
                }
              })
              follow_uid = follow_uids.join(',')
            }
          }
        }
        if (follow_uid && _.toString(follow_uid).length > 0) {
          followUrl = '/feact/wb_follow'
        }
      }
    }
    return {followUrl, follow_uid};
  }

}